<template>
    <div class="pageContainer">
        <div class="searchWarp">
            <template v-for="condition in listCondition">
                <CCondition :condition="condition"></CCondition>
            </template>
            <div class="schLine">
                <CBtn @click="search(1)">查询</CBtn>
            </div>
        </div>
        <div class="treeTable">
            <CCateTree :treeData="treeData" @nodeClick="nodeClick"></CCateTree>
            <div style="flex:1;">
                <div class="btnWarp">
                    <CBtn @click="showEdit">新增</CBtn>
                    <CBtn @click="doBatchDel()">批量删除</CBtn>
                </div>
                <CPageTable :data="tableData" :curPage="paging.curPage" :total="paging.total" 
                    @changeSelect="changeSelect"
                    @changeSize="changeSize" @changePage="search">
                    <template slot-scope="scope">
                        <CBtn type="text" @click="showCopy(scope.row)">复制</CBtn>
                        <CBtn type="text" @click="showEdit(scope.row)">修改</CBtn>
                        <CBtn type="text" @click="doDel(scope.row)">删除</CBtn>
                    </template>

                </CPageTable>
            </div>
        </div>

        <CDailog title="新增/编辑" :isShow="isShowEdit" @close="isShowEdit = false" @enter="doSave">
            <el-form :inline="true">
                <CText form v-model="mm.code" title="编码" field="code"></CText>
                <CText form v-model="mm.name" title="名称" field="name"></CText>
                <CSelect form v-model="mm.fkSqlCategory" title="分类" field="fk_sql_category" opType="opSqlTemplateType"></CSelect>
                <CSelect form v-model="mm.sqlType" title="类型" field="sql_type" opType="sqlType"></CSelect>
                <CTextarea form v-model="mm.sqlField" title="字段" field="sql_field"></CTextarea>
                <CTextarea form v-model="mm.tableName" title="表名" field="table_name"></CTextarea>
                <CTextarea form v-model="mm.sqlWhere" title="条件" field="sql_where"></CTextarea>
                <CText form v-model="mm.sqlEnd" title="结束" field="sql_end"></CText>
                <CText form v-model="mm.columnNames" title="字段中文" field="column_names"></CText>
                <CText form v-model="mm.connection" title="连接池" field="connection"></CText>
            </el-form>
        </CDailog>
    </div>
</template>

<script>
import pagingMixin from '@/mixin/pagingMixin'
export default {
    mixins: [pagingMixin],
    data() {
        return {
            treeData: [],
            model: "sys",
            table: "SysSqlTemplate"
        }
    },
    created() {
        this.templateId = "1";
        //查询树形
        this.searchTree();
        
        //取得查询条件
        this.$sys.getPageConditions({id: this.templateId}).then(res => {
            this.listCondition = res.data;
        })
        //查询表格数据
        this.search(1);
    },
    methods: {
        searchTree() {
            this.$sys.getTreeByCode({code: "template_type"}).then(res => {
                this.treeData = res.data;
            })
        },
        nodeClick(node) {
            this.otherCondition = [];
            this.$u.addWhere(this.otherCondition, "fk_sql_category", node.id);
            this.search(1, [...this.listCondition, ...this.otherCondition]);
        },
        showCopy(row) {
            this.$sys.getById(this.model, this.table, {id: row.id}).then(res => {
                this.mm = res.data;
                this.mm.id = "";
                this.isShowEdit = true;
            })
        }
    }
}
</script>

<style lang="scss" scoped></style>